Ext.namespace('ZZExt');

ZZExt.Application = Ext.extend(Ext.Viewport,{
	welcomeFrame:null,//欢迎页
	constructor:function(config){
		Ext.apply(this,config);
		
		this.workspace = new ZZExt.Workspace();
		//初始化管理菜单
		this.explorer = new Ext.tree.TreePanel({
			region : 'west',
			title : '管理项',
			iconCls:'app-west',
			width : 200,
			border : true,
			autoScroll : true,
			margins : '0 2 2 5',
			collapsible : true,
			collapseMode : 'mini',
			loader:new Ext.tree.TreeLoader({
				url:'systemMenu!fetchManageMenuTree.action'
			}),
			root : new Ext.tree.AsyncTreeNode({
				text : '<font color="#999999">所有栏目</font>',
				leaf : false,
				expanded:false,
				iconCls:'pkg'
			})
		});
		
		var _html='<div style="height:100%;background-image:url(images/topbg.png);line-height:30px;color:#FFFFFF;">';
		_html+='<div style="float:left;padding-left:10px"><img src="images/ChinaUnicom.png" style="height:50px"/></div>';
		_html+='<div class="sitetitle">无线宽带接入认证</div>';
		//_html+='<div style="padding:20p"'
		_html+='<div id="userinfoDiv" style="width:250px;text-align:right;padding:20px;float:right;font-size:12px;background-image:url(images/right.png)">初始化...</div>';
		_html+='</div>';
		this.userInfo=new Ext.Toolbar.TextItem({
	   		text:'系统尚未初始化，暂无系统用户信息'
	   });
	   
		var northCmp = new Ext.Panel({
	   		region:'north',
		    //height:70,
		    height:55,
	   		margins:'2 5 2 5',
         	html:_html
        });
		ZZExt.Application.superclass.constructor.call(this,{
			layout : 'border',
			items:[northCmp,this.explorer,this.workspace]
		});
		this.explorer.on('click', function(node, e){
	         if(node.isLeaf()){
	         	e.stopEvent();
	            this.workspace.loadClass(node.attributes.xtype, '_tab',{});
	            this.doLayout();
	         }
	    },this);
	    
		this.welcomeFrame = this.workspace.loadClass('framepanel','welcome',{
        	closable:false,
        	title:'欢迎使用',
        	id:'welcomepage',
        	src:'welcome.jsp'
//        	listeners:{
//        		'activate':function(){
//        			document.getElementById('frame-welcomepage').src='welcome.jsp?d='+new Date().getTime();
//        		}
//        	}
        });
	    this.refresh();
	},
	refresh:function(){
       this.initParams();
    },
    initParams:function(){
    	var user= ZZExt.ApplicationContext.getUser();
    	if(user == null){
	    	Ext.Ajax.request({
	    		url:'system!fetchCurrentOperator.action?d='+new Date().getTime(),
	    		method:'get',
	    		scope:this,
	    		success:function(response, opts){
	    		  var obj = Ext.decode(response.responseText);
	    		  
	    		  if(obj.success){
	    		  		var u = obj.data;
	    		  		if(u){
	    		  			//ZZExt.ApplicationContext.user=new User(u.id,u.username,u.role.code,u.role.name);
	    		  			//ZZExt.ApplicationContext.user=new User(u.id,u.username,'ADMIN','管理员');
	    		  			var role = u.role;
	    		  			
	    		  			var actions = [];
	    		  			for(var i=0;i<role.systemResources.length;i++){
	    		  				var p = role.systemResources[i];
	    		  				actions.push({
    		  						code:p.code
    		  					});
	    		  			}
	    		  			
	    		  			var user={
	    		  				id:u.id,
	    		  				username:u.username,
	    		  				role:{
	    		  					code:role.id,
	    		  					name:role.name,
	    		  					level:role.level,
	    		  					actions:actions
	    		  				}
	    		  			};
	    		  			ZZExt.ApplicationContext.setUser(user);
	    		  			this.resetUserInfo();
			    		  	return ;
	    		  		}
	    		  }
	    		  ZZExt.ApplicationContext.login();
	    		  
	    		}
	    	});
    	}
    	else {
    		this.resetUserInfo(user);
    	}
    },
    resetUserInfo:function(){
    	var user = ZZExt.ApplicationContext.getUser();
    	var root = this.explorer.getRootNode();
    	root.reload(function(){
    		root.expandChildNodes();
    	});
    	this.explorer.getRootNode().expandChildNodes(true)
    	var _html=' 欢迎  <font color="yellow">'+user.username+'</font> 登陆系统!';
    	_html+='&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="ZZExt.Application.logOut()" style="color:white;text-decoration: none;">退出</a>';
    	//_html=_html+'&nbsp;&nbsp;&nbsp;&nbsp;所属角色(用户类型):<font color="#669900">'+user.roleName+'</font>';
        //this.userInfo.setText(_html);
    	document.getElementById('userinfoDiv').innerHTML = _html;
    	this.welcomeFrame.setSrc('welcome.jsp?d='+new Date().getTime());
	}
});
ZZExt.Application.logOut = function(){
	Ext.Ajax.request({
		url:'operator!logout.action',
		success: function(response,opts){
			var obj = Ext.decode(response.responseText);
			if(obj.success){
				Ext.Msg.alert('系统提示','您已退出系统！');
				window.location.href='adminLogin.jsp';
			} else {
				Ext.Msg.alert('系统提示','出错:'+obj.data);
			}
	         
        }
	});
}
ZZExt.ApplicationContext={
   user:{
	   username:'',
	   role:{
		   code:'',
		   name:'',
		   actions:[]
	   }
   },
   
   loginWindow:null,
   applicationViewport:null,
   actions:[],//{clientClass:'',clientMethod:'method'}
   getControlKey:function(clazz,control){
	   return 'key_class_'+clazz+'_control_'+control;
   },
   actionAccessable:function(code){
	   for(var i=0;i<this.actions.length;i++){
		   if(new RegExp(this.actions[i].code).test(code)){
			   
			    return true;
		   }
	   }
	   return false;
	   //var flag = this.controlMap[this.getControlKey('*','*')]||this.controlMap[this.getControlKey('*',control)]||this.controlMap[this.getControlKey(clazz,'*')];
	   //return flag||this.controlMap[this.getControlKey(clazz,control)];
   },
   setUser:function(user){
	   this.user = user;
	   if(this.user==null||this.user.role==null||this.user.role.actions==null) return;
	   this.actions = this.user.role.actions;
	   
	   //for(var i=0;i<actions.length;i++){
		  //this.controlMap[this.getControlKey(actions[i].clientClass, actions[i].clientMethod)]=true;
	   //}
   },
   
   
   getUser:function(){
		return this.user;
   },
   login:function(){
	   var win = this.getLoginWindow();
	   win.show();
	   win.init(this.getUser());
   },
   getLoginWindow:function(){
		if(this.loginWindow==null){
			this.loginWindow=new ZZExt.LoginWindow({
				modal:true,
				doLoginSuccess:this.initApplicationContext,
				scope:this
			});
		}
		return this.loginWindow;
   },
   getApplicationViewport:function(){
   		if(this.applicationViewport==null) {
   			this.applicationViewport=new ZZExt.Application();
   		}
   		return this.applicationViewport;
   },
   initApplicationContext:function(form,action){
   		var _this=this.scope;
   		var r=action.result;
   		if(r.success&&r.user){
   			//_this.user=new User(r.user.id,r.user.name,r.user.role,r.user.type);
   		}
   		var _this=this.scope;
   		_this.getLoginWindow().hide();
   		_this.getApplicationViewport().refresh();
   },
   openTab:function(xtype,params){
	  return this.getApplicationViewport().workspace.loadClass(xtype, '_tab',params);
   },
   closeTab:function(xtype){
	   this.getApplicationViewport().workspace.removeClass(xtype, '_tab');
   },
   removeTabCmp:function(cmp){
	   this.getApplicationViewport().workspace.remove(cmp);
   },
   
   startRun:function(){
   		Ext.BLANK_IMAGE_URL="ext/resources/images/default/s.gif"; 
	    Ext.MessageBox.minWidth=200;
	    Ext.QuickTips.init();
	    this.setUser(null);
		this.getApplicationViewport().show();
   }
	
}

	